﻿/*
 * This file is part of SCWS.
 * SCWS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 * 
 * Foobar is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with SCWS.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml;
using System.Xml.Schema;
using System.Data;
using System.Globalization;
using System.Security.Principal;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Configuration;
using Microsoft.EnterpriseManagement.Administration;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Monitoring;



namespace Scws
{

    public partial class Om : System.Web.Services.WebService
    {

        //private string XSD_MAINTENANCEWINDOW
        //{
        //    get
        //    {
        //        string results = Server.MapPath("~") + "\\xsd\\MaintenanceWindow.xsd";
        //        return results;
        //    }
        //}

        private string XSD_PARTIALMONITORINGOBJECT
        {
            get
            {
                string results = Server.MapPath("~") + "\\xsd\\PartialMonitoringObject.xsd";
                return results;
            }
        }

        private string XSD_MAINTENANCEMODERESULTS
        {
            get
            {
                string results = Server.MapPath("~") + "\\xsd\\MaintenanceModeResults.xsd";
                return results;
            }
        }


        /// <summary>
        /// Creates a datatable from the provided readonly collection
        /// Uses the XML schema file
        /// </summary>
        /// <param name="objects">Readonly collection of type: partialmonitoringobject</param>
        /// <returns>DataTable</returns>
        private DataSet OutPartialMonitoringObject(ReadOnlyCollection<PartialMonitoringObject> objects)
        {
            DataSet ds = new DataSet();

            try
            {
                ds.ReadXmlSchema(XSD_PARTIALMONITORINGOBJECT);
            }
            catch
            {
                ds.Dispose();
                throw new Scws.Om.XsdNotFoundException(XSD_PARTIALMONITORINGOBJECT);
            }

            foreach (PartialMonitoringObject o in objects)
            {
                DataRow row = ds.Tables["PartialMonitoringObject"].NewRow();
                if (o.AvailabilityLastModified == null)
                    row["AvailabilityLastModified"] = DBNull.Value;
                else
                    row["AvailabilityLastModified"] = o.AvailabilityLastModified;
                row["DisplayName"] = o.DisplayName;
                row["FullName"] = o.FullName;
                row["HealthState"] = o.HealthState.ToString();
                row["Id"] = o.Id;
                row["InMaintenanceMode"] = o.InMaintenanceMode;
                row["IsAvailable"] = o.IsAvailable;
                row["LastModified"] = o.LastModified;
                row["LeastDerivedNonAbstractMonitoringClassId"] = o.LeastDerivedNonAbstractMonitoringClassId;
                if (o.MaintenanceModeLastModified == null)
                    row["MaintenanceModeLastModified"] = DBNull.Value;
                else
                    row["MaintenanceModeLastModified"] = o.MaintenanceModeLastModified;
                row["ManagementGroupId"] = o.ManagementGroupId;
                // Removed because .Net DataSet does not support list as a simpletype
                // There will be a seperate method to get all monitoring classes
                //
                //row["MonitoringClassIds"] = o.MonitoringClassIds;
                //
                row["Name"] = o.Name;
                row["Path"] = o.Path;
                if (o.StateLastModified == null)
                    row["StateLastModified"] = DBNull.Value;
                else
                    row["StateLastModified"] = o.StateLastModified;
                ds.Tables["PartialMonitoringObject"].Rows.Add(row);
            }
            return ds;
        }




        ///// <summary>
        ///// Creates a datatable from the provided readonly collection
        ///// Uses the XML schema file
        ///// </summary>
        ///// <param name="objects">Collection of type: MaintenanceWindow</param>
        ///// <returns>DataTable</returns>
        //private DataSet OutMaintenanceWindow(Collection<MaintenanceWindow> objects)
        //{
        //    DataSet ds = new DataSet();

        //    try
        //    {
        //        ds.ReadXmlSchema(XSD_MAINTENANCEWINDOW);
        //    }
        //    catch
        //    {
        //        ds.Dispose();
        //        throw new Scws.Om.XsdNotFoundException(XSD_MAINTENANCEWINDOW);
        //    }

        //    foreach (MaintenanceWindow o in objects)
        //    {
        //        DataRow row = ds.Tables["MaintenanceWindow"].NewRow();
        //        row["Comments"] = o.Comments;
        //        if (o.EndTime == null)
        //            row["EndTime"] = DBNull.Value;
        //        else
        //            row["EndTime"] = o.EndTime;
        //        row["LastModified"] = o.LastModified;
        //        row["ManagementGroupId"] = o.ManagementGroupId.ToString();
        //        row["MonitoringObjectId"] = o.MonitoringObjectId.ToString();
        //        row["Reason"] = o.Reason.ToString();
        //        row["ScheduledEndTime"] = o.ScheduledEndTime;
        //        row["StartTime"] = o.StartTime;
        //        row["user"] = o.User;
        //        ds.Tables["MaintenanceWindow"].Rows.Add(row);
        //    }
        //    return ds;
        //}

    }
}